<?php
/**
 * 工作
 *
 * @package ews
 * @ews Enterprise WorkShop
 * @version 1.0 alpha
 * @since 2011-05-06
 * @author monkee
 */

class workApp extends app
{
	function __construct(){
		$this->_init();
		$this->login_need();
	}

	public function index(){
		$workmod = $this->getModel('work');
		$todomod = $this->getModel('todo');
		return array(
			'unfin' => $workmod->get_works_by_user_id_and_state($this->user_id, 0, 10000, 1),
			'unfinished' =>$todomod->get_unfinished_todos($this->user_id)

		);
	}
	public function work(){
		$todomod = $this->getModel('work');
		return array(
			'finished' => $todomod->get_finished_works($this->user_id),
			'unfinished' => $todomod->get_unfinished_works($this->user_id)
		);
	}

	public function view(){
		$id = & $this->getData('get/id');
		$id = intval($id);
		if($id == 0){
			$this->message('错误的访问请求！');
		}
		$workmod = $this->getModel('work');
		$datas = $workmod->get_work($id);
		return array('datas' => $datas);
	}

	public function add(){
		$data = $this->getData('post');
		
		if(empty($data)){
			$obj = & $this->getModel('user');
			$users = $obj->get_all_users();
			return array('users' => $users);
		}
		$user_id = intval($data['user_id']);
		if($user_id == 0){
			$type = 'todo';
		}else{
			$type = 'work';
		}
		if($data['subject'] == ''){
			$data['subject'] = date('Y-m-d').' ';
			if($type == 'todo'){
				$data['subject'] .= '待办';
			}else{
				$data['subject'] .= '工作';
			}
		}
		if(empty($data['end_date'])){
			$data['end_date'] = '0000-00-00';
		}
		$work = & $this->getModel('work');
		if($type == 'todo'){
			$todomod = $this->getModel('todo');
			$r = $todomod->add(
				$this->user_id,
				$data['subject'],
				$data['message'],
				$data['end_date'],//截至日期
				$data['state']
			);
			go_to('/work/viewtodo?id='.$r, '添加成功');
		}else{
			$workmod = & $this->getModel('work');
			//($user_id, $end_time, $subject, $message, $mk_user_id, $mk_username)
			$r = $workmod->add(
				$user_id,
				$data['end_date'],
				$data['subject'],
				$data['message'],
				$this->user_id,
				$this->user['username']
			);
			//添加通知
			$notice = &$this->getModel('notice');
			$notice->create($user_id, "<a href=\"#\">{$this->user['username']}</a> 给你安排了工作：<a href=\"/work/view?id={$r}\" target=\"_blank\">{$data['subject']}</a>");
			go_to('/work/view?id='.$r, '添加成功');
		}
	}
	/**
	 * 查看待办
	 */
	public function viewtodo(){
		$id = & $this->getData('get/id');
		$id = intval($id);
		if($id == 0){
			$this->message('错误的访问请求！');
		}
		$todomod = $this->getModel('todo');
		$datas = $todomod->get_todo($id);
		return array('datas' => $datas);
	}
	
	public function fintodo(){
		$id = & $this->getData('get/id');
		$id = intval($id);
		if($id == 0){
			$this->message('错误的访问请求！');
		}
		$todomod = $this->getModel('todo');
		$todomod->finish_todo($id);
		$this->inner_msg('待办更新成功');
		go_to('/work/viewtodo?id='.$id,'待办状态更新成功');
	}
	
	public function deltodo(){
		$id = & $this->getData('get/id');
		$id = intval($id);
		if($id == 0){
			$this->message('错误的访问请求！');
		}
		$todomod = $this->getModel('todo');
		$todomod->delete_todo($id);
		go_to('/work/todo','删除待办');
	}
	
	public function todo(){
		$todomod = $this->getModel('todo');
		return array(
			'finished' => $todomod->get_finished_todos($this->user_id),
			'unfinished' => $todomod->get_unfinished_todos($this->user_id)
		);
	}
	
	public function workpercent(){
		$id = & $this->getData('get/id');
		$id = intval($id);
		if($id == 0){
			$this->inner_msg('错误的访问请求！');
		}
		$p = & $this->getData('get/p');
		$p = $p%100;
		$p = intval($p);
		$mod = & $this->getModel('work');
		
		$work = $mod->get_work($id);
		$notice = &$this->getModel('notice');
		$notice->create($work['mk_user_id'], "<a href=\"#\">{$this->user['username']}</a> 更新了工作进度：<a href=\"/work/view?id={$id}\" target=\"_blank\">{$work['subject']}</a>");
		
		$mod->set_finish_percent($id, $p);
		return array();
	}
	
	public function closework(){
		$id = & $this->getData('get/id');
		$id = intval($id);
		if($id == 0){
			$this->inner_msg('错误的访问请求！');
		}
		$mod = & $this->getModel('work');
		
		$work = $mod->get_work($id);
		$notice = &$this->getModel('notice');
		$notice->create($work['user_id'], "<a href=\"#\">{$this->user['username']}</a> 关闭了工作：<a href=\"/work/view?id={$id}\" target=\"_blank\">{$work['subject']}</a>");
		
		$mod->set_state($id, 2);
		go_to('/work/view?id='.$id, '工作状态更新');
	}
	
	public function finishwork(){
		$id = & $this->getData('get/id');
		$id = intval($id);
		if($id == 0){
			$this->inner_msg('错误的访问请求！');
		}
		$mod = & $this->getModel('work');
		//编辑通知
		$work = $mod->get_work($id);
		$notice = &$this->getModel('notice');
		$notice->create($work['mk_user_id'], "<a href=\"#\">{$this->user['username']}</a> 完成了工作：<a href=\"/work/view?id={$id}\" target=\"_blank\">{$work['subject']}</a>");
		
		$mod->set_state($id, 1);
		go_to('/work/view?id='.$id, '工作状态更新');
	}
	
	
	public function finishtodo(){
		$post = $this->getData('post');
		if(empty($post)){
			return array();
		}
		$post['todo_id'] = intval($post['todo_id']);
		$todomod = $this->getModel('todo');
		$r = $todomod->finish_todo($post['todo_id']);
		return array('msg' => '修改成功' , 'id' => 'todo_li_'.$post['todo_id']);
	}
	
	



	/*
	 public function add(){
		$act = $this->getData('post/act');
		if($act){
		$relate_users = $this->getData('post/relate_user');
		$subject = $this->getData('post/subject');
		$message = $this->getData('post/message');
		$workobj = & $this->getModel('work');
			
		$row = $workobj->check_users($relate_users);

		$relate_users = $row['usernames'];
		$userids = $row['user_ids'];
			
		$work_id = $workobj->create($subject, $message, $relate_users, $this->user_id);
			
		$noticeobj = & $this->getModel('notice');
		foreach($userids as $user_id){
		$notice_row = $noticeobj->notice_data(3, $work_id);
		$noticeobj->create($user_id, 3, '<a href="javascript:;">'.$this->user['username'].'</a> '.$notice_row['message']);
		}
			
		return array('act' => $act);
		}else{
		$users = $this->getData('post/users');
		$workobj = & $this->getModel('work');
		$row = $workobj->check_users($users);
		$relate_users = $row['usernames'];
		if(empty($relate_users)){
		$this->message('要分配的用户不存在', 'error');
		}
		return array('relate_users' => $relate_users);
		}
		}

		public function view(){
		$work_id = & $this->getData('post/work_id');
		$workobj = & $this->getModel('work');
		$work = $workobj->get($work_id);
		$todos = $workobj->get_todos_by_work_id($work_id);
		return array(
		'work' => $work,
		'todos' => $todos,
		'is_work_owner' => ($work['user_id'] == $this->user_id),
		'is_todo_state' => $workobj->get_todo_state($work_id, $this->user_id)
		);
		}

		public function finish(){
		$work_id = & $this->getData('post/work_id');
		$workobj = & $this->getModel('work');
		$workobj->finish($work_id, $this->user_id);

		$noticeobj = & $this->getModel('notice');

		$notice_row = $noticeobj->notice_data(4, $work_id);
		$noticeobj->create($notice_row['user_id'], 4, '<a href="javascript:;">'.$this->user['username'].'</a> '.$notice_row['message']);

		return array('code' => '1');
		}
		*/
}
?>